/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 - 2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/

package com.genia.toolbox.basics.bean;

import java.util.Comparator;

import com.genia.toolbox.basics.process.Comparators;

/**
 * compare two arrays using first the first value and going on if the values are
 * equals. A smaller array, is always before a bigger one.
 * 
 * @param <TYPE>
 *          the type of the elements of the arrays
 */
public class ArrayComparator<TYPE>
    implements Comparator<TYPE[]>
{

  /**
   * the {@link Comparator} used to compare elements of the array.
   */
  private Comparator<? super TYPE> baseComparator;



  /**
   * constructor.
   */
  public ArrayComparator()
  {
    this(null);
  }



  /**
   * constructor.
   * 
   * @param baseComparator
   *          the {@link Comparator} used to compare elements of the array.
   */

  @SuppressWarnings("unchecked")
  public ArrayComparator(Comparator<? super TYPE> baseComparator)
  {
    this.baseComparator = (Comparator<? super TYPE>) (baseComparator == null ? Comparators.NATURAL_COMPARATOR : baseComparator);
  }



  /**
   * Compares its two arguments for order. Returns a negative integer, zero, or
   * a positive integer as the first argument is less than, equal to, or greater
   * than the second.
   * <p>
   * 
   * @param array1
   *          the first object to be compared.
   * @param array2
   *          the second object to be compared.
   * @return a negative integer, zero, or a positive integer as the first
   *         argument is less than, equal to, or greater than the second.
   * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
   */
  public int compare(TYPE[] array1, TYPE[] array2)
  {
    int res;
    res = array1.length - array2.length;
    if (res != 0) {
      return res;
    }
    LOOP: for (int i = 0; i < array1.length; i++) {
      if (array1[i] == null) {
        if (array2[i] != null) {
          return -1;
        }
        continue LOOP;
      }
      if (array2[i] == null) {
        return 1;
      }

      res = baseComparator.compare(array1[i], array2[i]);
      if (res != 0) {
        return res;
      }
    }
    return 0;
  }

}
